package com.ncs.cns.activity.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ToggleButton;
import cn.jpush.android.api.JPushInterface;

import com.ncs.cns.R;
import com.ncs.cns.framework.Constant;

public class SettingFragment extends Fragment{
	private View view;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.activity_setting, null);
		final ToggleButton tb = (ToggleButton)view.findViewById(R.id.toggleButton);
		final SharedPreferences sp = view.getContext().getSharedPreferences(Constant.SHAREPATH,
				Context.MODE_PRIVATE);
		boolean flag = sp.getBoolean(Constant.ACCEPT_PUSH, true);
		tb.setChecked(flag);
		
		tb.setOnClickListener(new OnClickListener(){

			public void onClick(View arg0) {
				Editor editor = sp.edit();
				editor.putBoolean(Constant.ACCEPT_PUSH, tb.isChecked());
				editor.commit();	
				if(!sp.getBoolean(Constant.ACCEPT_PUSH, true)){
					JPushInterface.stopPush(view.getContext());
				}else{
					JPushInterface.resumePush(view.getContext());
				}
			}
			
		});
		
		/*ListView listView = (ListView)view.findViewById(R.id.listView);
		
		List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>();
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("item", getString(R.string.acceptPush));
		data.put("summary", getString(R.string.acceptPushSummary));
		datas.add(data);
		
		data = new HashMap<String, Object>();
		data.put("item", getString(R.string.downloadPic));
		data.put("summary", getString(R.string.downloadPicSummary));
		datas.add(data);
		
		listView.setAdapter(new SimpleAdapter(this.getActivity(), datas, R.layout.setting_list_item,
				new String[]{"item", "summary"},
				new int[]{R.id.itemsTitle, R.id.itemsText}));
		
		listView.setOnItemClickListener(new OnItemClickListener(){

			public void onItemClick(AdapterView<?> arg0, View item, int index,
					long arg3) {
				
				ToggleButton tb = (ToggleButton)item.findViewById(R.id.toggleButton);
				tb.toggle();
				
				SharedPreferences  sp = getActivity().getSharedPreferences(Constant.SHAREPATH, Context.MODE_PRIVATE);
				Editor editor = sp.edit();
				if(index == 0){
					editor.putBoolean(Constant.ACCEPT_PUSH, tb.isChecked());
				}else{
					editor.putBoolean(Constant.DOWNLOAD_PIC, tb.isChecked());
				}
				editor.commit();	
				if(!sp.getBoolean(Constant.ACCEPT_PUSH, true)){
					JPushInterface.stopPush(view.getContext());
				}
			}			
		});*/
		
		return view;
	}
}

